package com.map;

import java.util.HashMap;
import java.util.Map;

public class MapDemo1 {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        map.put("e", 5);
        map.put("f", 6);
        map.put("g", 7);
        System.out.println("map = " + map);

        //如果键存在，则返回旧值，否则返回null
        Integer value1 = map.put("h", 10);
        System.out.println("value = " + value1);
        Integer value2 = map.put("b", 10);
        System.out.println("value = " + value2);

        //如果键存在，则更新值，否则添加
        map.put("a", 8);
        System.out.println("map = " + map);

        //根据key获取value
        System.out.println(map.get("a"));
        System.out.println(map.get("b"));
        System.out.println(map.get("c"));

        //获取map中键值对的个数
        System.out.println(map.size());

        //删除键值对,返回的是被删除的value
        Integer value3 = map.remove("g");
        System.out.println("删除的元素对应的值:" + value3);
        System.out.println("map = " + map);

        //判断集合是否为空
        boolean empty = map.isEmpty();
        System.out.println("判断集合是否为空 = " + empty);

        //判断集合中是否包含指定的键或者值
        boolean ck = map.containsKey("a");
        System.out.println("ck = " + ck);
        boolean cv = map.containsValue(30);
        System.out.println("cv = " + cv);

        //清空集合
        map.clear();
        System.out.println("map = " + map);

    }
}
